package cc.wsyw126.java.nowcoder;


import java.util.ArrayList;
import java.util.Stack;


public class PrintListFromTailToHead {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList<Integer> result = new ArrayList<>();
        if (listNode == null) return result;
        Stack<ListNode> s = new Stack<>();
        while (listNode != null) {
            s.push(listNode);
            listNode = listNode.next;
        }
        while (!s.isEmpty()) {
            ListNode pop = s.pop();
            result.add(pop.val);
        }
        return result;
    }


}

class ListNode {
    int val;
    ListNode next = null;
    ListNode(int val) {
        this.val = val;
    }
}

